<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/flow/condition/conditionList' }">条件列表</el-breadcrumb-item>
      <el-breadcrumb-item>条件信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="类型" prop="type">
                <el-select v-model="editForm.type">
                  <el-option label="其它" value="0" />
                  <el-option label="流转" value="1" />
                  <el-option label="会签" value="2" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="值" prop="value">
                <el-input v-model="editForm.value" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { viewCondition, saveCondition, updateCondition } from 'src/api/flow/condition'

// 定义表单数据
const editFormRef = ref<FormInstance>()
const editForm = reactive({
  id: '',
  name: '',
  type: '0',
  value: '',
  remark: ''
})
const editFormRules = {
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择类型', trigger: 'change' }
  ],
  value: [
    { required: true, message: '请输入值', trigger: 'blur' }
  ]
}

// 修改时填充表单数据
const route = useRoute()
if (route.query.id) {
  const id = route.query.id as string
  viewCondition(id).then(({ data }) => Object.assign(editForm, data))
}

// 提交
const submit = function (formEl: FormInstance | undefined) {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveCondition(editForm).then(() => {
        cancel()
      })
    } else {
      updateCondition(editForm).then(() => {
        cancel()
      })
    }
  })
}

// 关闭
const router = useRouter()
const cancel = function () {
  router.push('/flow/condition/conditionList')
}
</script>

<style lang="scss" scoped>
</style>
